package com.macro.mall.portal.controller;


import com.baomidou.mybatisplus.annotation.TableField;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.*;
import com.macro.mall.portal.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 前端控制器
 *
 * @author db
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/heat")
@Api(tags = "测额列表", description = "测额列表")
public class HeatController {

    @Autowired
    private HeatService heatService;

    @Autowired
    private CsCarService csCarService;

    @Autowired
    private CsHouseService csHouseService;

    @Autowired
    private CsSalaryService csSalaryService;

    @Autowired
    private CsTaxiService csTaxiService;

    @ApiOperation("测额列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list() {
        try {
            List<Heat> list = heatService.list().stream().sorted(Comparator.comparing(Heat::getTotal).reversed()).collect(Collectors.toList());
            list.stream().forEach(item -> {
                item.setTotal(item.getTotal() + 1);
                Heat heat = Heat.builder().id(item.getId()).total(item.getTotal()).build();
                heatService.updateById(heat);
            });
            return CommonResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("添加车测额")
    @Transactional
    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    public CommonResult add(@RequestBody CsCar csCar) {
        try {
            csCarService.save(csCar);
            BigDecimal decimal = new BigDecimal(csCar.getMoney());
            Map<String, Object> map = new HashMap<>();
            BigDecimal multiply = decimal.multiply(new BigDecimal(0.8));
            map.put("money", multiply);
            return CommonResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("添加房测额")
    @Transactional
    @RequestMapping(value = "/addHouse", method = RequestMethod.POST)
    public CommonResult addHouse(@RequestBody CsHouse csHouse) {
        try {
            csHouseService.save(csHouse);
            BigDecimal decimal = new BigDecimal(csHouse.getMoney());
            Map<String, Object> map = new HashMap<>();
            BigDecimal multiply = decimal.multiply(new BigDecimal(0.8));
            map.put("money", multiply);
            return CommonResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("添加工薪测额")
    @Transactional
    @RequestMapping(value = "/addSalary", method = RequestMethod.POST)
    public CommonResult addSalary(@RequestBody CsSalary csSalary) {
        try {
            csSalaryService.save(csSalary);
            BigDecimal decimal = new BigDecimal(csSalary.getWages());
            Map<String, Object> map = new HashMap<>();
            BigDecimal multiply = decimal.multiply(new BigDecimal(20));
            map.put("money", multiply);
            return CommonResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("添加税测额")
    @Transactional
    @RequestMapping(value = "/addTaxi", method = RequestMethod.POST)
    public CommonResult addTaxi(@RequestBody CsTaxi csTaxi) {
        try {
            csTaxiService.save(csTaxi);
            BigDecimal decimal = new BigDecimal(csTaxi.getTotalTax());
            Map<String, Object> map = new HashMap<>();
            BigDecimal multiply = decimal.multiply(new BigDecimal(20));
            map.put("money", multiply);
            return CommonResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }


}
